/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-24 23:14:31
 * @LastEditTime: 2022-07-27 18:08:34
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper3




void H_NES_Mapper3_PRG_Write_NROM128(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGWriteAccess,Address);
      // for(;;){
      // }
      return;
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGRAMWriteAccess,Address);
      // for(;;){
      // }
      return;
    }

    p[Address-0x6000U]=Value;
  }else{
    Huint32 ROM_Size;
    ROM_Size=Console->Cartridge.Mapper.CHR_ROM_Size;
    if(ROM_Size==0U){
      ROM_Size=0x2000U;
    }
    Console->ThisMapper.CHR_Banks.Base0000_8KB=&Console->Cartridge.Mapper.CHR_ROM[(((Huint32)Value)*0x2000U)%ROM_Size];
  }
}




void H_NES_Mapper3_PRG_Write_NROM256(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGWriteAccess,Address);
      // for(;;){
      // }
      return;
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGRAMWriteAccess,Address);
      
      // for(;;){
      // }
      return;
    }

    p[Address-0x6000U]=Value;
  }else{
    Huint32 ROM_Size;
    ROM_Size=Console->Cartridge.Mapper.CHR_ROM_Size;
    if(ROM_Size==0U){
      ROM_Size=0x2000U;
    }
    Console->ThisMapper.CHR_Banks.Base0000_8KB=&Console->Cartridge.Mapper.CHR_ROM[(((Huint32)Value)*0x2000U)%ROM_Size];
  }
}




void H_NES_Mapper3_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionCHRWriteAccess,Address);
}
void H_NES_Mapper3_CHR_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  Console->ThisMapper.CHR_Banks.Base0000_8KB[Address]=Value;
}
Hbyte H_NES_Mapper3_CHR_Read(Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  return Console->ThisMapper.CHR_Banks.Base0000_8KB[Address];
}




#undef ThisMapper



